package com.thomasmoerman.dci3.domain

import java.util.{Set => JSet}
import java.util.HashSet
import com.thomasmoerman.dci3.frw.{Entity => SEntity}
import javax.persistence._
import org.hibernate.Session

import org.hibernate.annotations.CascadeType._
import org.hibernate.annotations.Cascade

import com.thomasmoerman.dci3.tmc._
import com.thomasmoerman.dci3.domain._

@Entity
class Account extends SEntity with MoneySink {
  
  def this(owner: Person) {
	this()
	
	this.owner = owner
  }
	
  @OneToOne var owner: Person = _
  
  @OneToMany(mappedBy = "account") @Cascade(Array(MERGE, SAVE_UPDATE))
  var moneyTransfers: JSet[MoneyTransfer] = new HashSet[MoneyTransfer]
  
  def increaseBalance(amount: Double): Unit = moneyTransfers.add(Increase(this, amount))
  
  def decreaseBalance(amount: Double): Unit = moneyTransfers.add(Decrease(this, amount))
  
  def updateLog(msg: String, amount: Double) = println(this + " -> Account " + id + " " + msg + " " + amount)
  
}